package com.daydr3am.thenation;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.w3c.dom.Node;

import com.daydr3am.lib.CustomView;
import com.daydr3am.lib.TouchyWebView;
import com.eightbitcloud.pagingscroller.Pager;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.webkit.WebSettings;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.TextView;

public class TheNationMain extends Activity {
    private Pager scroller;
    boolean subclick;
    int orientation;
    static int[][] Col={{3,65,145},{59,132,62},{50,28,150},{11,132,182},{93,128,145},{156,41,49},{218,128,11},{172,3,140},{215,209,7}};
	OnTouchListener headSectionEvent=new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if(event.getAction()==MotionEvent.ACTION_UP){
				Log.v("hello", "act up");
				int index=(int) (event.getX())/(v.getWidth()/9);
		        FrameLayout sectionColor = ((FrameLayout)findViewById(R.id.sectionColor));
		        sectionColor.setBackgroundColor(Color.rgb(Col[index][0], Col[index][1], Col[index][2]));

		        Pager page=(Pager) findViewById(R.id.bigscroll);
		        String[] map = getResources().getStringArray(R.array.maping);
		        for(int i=0;i<map.length;i++){
		        	Log.v("hello",index+ " "+map[i]+" "+i);
		        	if(Integer.parseInt( map[i])==index+1){
		        		if(i!=0){
		        			page.scrollToPage(i-1);
		        			Log.v("hello", "scroll here "+(i-1));
		        		}
		        		else
		        			page.scrollToPage(0);
		        			
		        		break;
		        	}
		        }
		        
			}
			return true;
		}
	};
	OnTouchListener footEvent=new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if(event.getAction()==MotionEvent.ACTION_UP){
		        Pager page=(Pager) findViewById(R.id.bigscroll);
		        String[] map = getResources().getStringArray(R.array.maping);
				float xPosition=event.getX();
				if(xPosition>30&&xPosition<70&&page.getCurrentPage()>0){
        			page.scrollToPage(page.getCurrentPage()-1);
				}
				if(xPosition<page.getWidth()-30&&xPosition>page.getWidth()-70&&page.getCurrentPage()<map.length-1)
        			page.scrollToPage(page.getCurrentPage()+1);
					
			}
			return true;
		}
	};

	@Override
	public void onResume(){
		super.onResume();
    	Date date=new Date();
    	SimpleDateFormat format=new SimpleDateFormat("EE,MMMM dd,yyyy");
    	TextView dateShow=(TextView) findViewById(R.id.showdate);
    	dateShow.setText(format.format(date));
    	
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thenationmain);
        
        scroller = ((Pager)findViewById(R.id.bigscroll));
        HorizontalScrollView sectionMenu = ((HorizontalScrollView)findViewById(R.id.sectionScrollView));
        FrameLayout sectionColor=(FrameLayout) findViewById(R.id.sectionColor);
        sectionColor.setBackgroundColor(Color.rgb(Col[0][0], Col[0][1], Col[0][2]));
        View view=sectionMenu.getChildAt(0); 
        view.setOnTouchListener(headSectionEvent);
        findViewById(R.id.footmenu).setOnTouchListener(footEvent);
        
        
        sectionMenu.setOnTouchListener(new OnTouchListener() {
        	private boolean scroll;
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				if(event.getAction()==MotionEvent.ACTION_DOWN){
					scroll=false;
				}
				if(event.getAction()==MotionEvent.ACTION_MOVE){
					scroll=true;
				}
				if(event.getAction()==MotionEvent.ACTION_UP&&!scroll){
					View image=findViewById(R.id.sectionImage);
					int touchObject =(int) ((v.getScrollX()+event.getX())/(image.getWidth()/9));
					Log.v("hello", "touch object "+touchObject);
				}
				return false;
			}
		});
        
        LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        String[] content = getResources().getStringArray(R.array.url_feed);
        String[] fullad = getResources().getStringArray(R.array.url_fullad);
        
        for (int i = 0; i < content.length; i++) {
        	if(content[i].length()>5){	
        		View pageView = layoutInflater.inflate(R.layout.custommainlayout, null);
        		((CustomView) pageView).index=i;
        		
        		scroller.addPage(pageView);
        	}
        	else{
	        	TouchyWebView web=new TouchyWebView(this);
	        	if(getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE)
	        		web.url="http://www.nationmultimedia.com/home/banner/ipad-banner/Nation/Full-Page-Nation/H"+fullad[i];
	        	else
	        		web.url="http://www.nationmultimedia.com/home/banner/ipad-banner/Nation/Full-Page-Nation/V"+fullad[i];
	        		
	        	scroller.addPage(web);
	        	web.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
	    	    if(i==0)
	    	    	web.onShowView();
        	}
        }
       
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		int height = displaymetrics.heightPixels;
		int width = displaymetrics.widthPixels;
		Log.v("hello", width+" code "+height);
       
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_thenationmain, menu);
        return true;
    }
    
    public void scrollMain(int pageIndex){
    	backToHome();
        String[] map = getResources().getStringArray(R.array.maping);
        int section=Integer.parseInt( map[pageIndex])-1;
        Log.v("hello", "section up "+section);
        if(section==-1&&pageIndex!=0)
        	section=Integer.parseInt( map[pageIndex+1])-1;
        Log.v("hello", "section color "+section);
    	FrameLayout sectionColor = ((FrameLayout)findViewById(R.id.sectionColor));
    	sectionColor.setBackgroundColor(Color.rgb(Col[section][0], Col[section][1], Col[section][2]));
    }

    public void clickNews(Node obj){
    	findViewById(R.id.bigscroll).setVisibility(View.INVISIBLE);
    	Log.v("hello", "test "+findViewById(R.id.material_layout));
    	findViewById(R.id.material_layout).setVisibility(View.VISIBLE);
    	
    	Material control=(Material) findViewById(R.id.material_control);
    	control.setObj(obj);
    	
    }
    public void backToHome(){
    	findViewById(R.id.material_layout).setVisibility(View.INVISIBLE);
    	findViewById(R.id.bigscroll).setVisibility(View.VISIBLE);
    }
    
    
    
    
    public static int getSectionFromPage(int page,Context con){
        String[] map = con.getResources().getStringArray(R.array.maping);
        int section=Integer.parseInt( map[page])-1;
    	return section;
    }
    public static int getColor(int pageIndex,Context con){
        int section=getSectionFromPage(pageIndex,con);
        if(section==-1){
        	return Color.WHITE;
        }
        return Color.rgb(Col[section][0], Col[section][1], Col[section][2]);
    }
}
